/// Creator:      gaoruishan
/// CreatDate:    2025-06-25
/// Description:  交班查询  
Class Nur.MNISV3.Service.Shift Extends %RegisteredObject [ Not ProcedureBlock ]
{

/// 查询
/// w ##class(Nur.MNISV3.Service.Shift).GetNursingBookDataV4("","2025-06-24","8","20665","3","2","{""single"":"""",""screening"":"""",""book"":"""",""multiple"":""1"",""class"":""""}")
ClassMethod GetNursingBookDataV4(episodeId, date, locId, userId, wardId, hospitalId, extras)
{
    n (episodeId, date, locId, userId,wardId,hospitalId,extras,%session)
    s $zt="errGetNursingBookDataV4"
    s ^GRS("GetNursingBookDataV4")=$lb(episodeId, date, locId, userId,wardId,hospitalId,extras)
    s Msg="成功",msgcode="999999",Status="0" //定义初始值
    s json=##class(%ArrayOfDataTypes).%New()
    s date=##class(websys.Conversions).DateHtmlToLogical(date)
    s bookItems=..GetBookItems(hospitalId,wardId,.defaultBookId)
    s classItems=..GetClassItems(hospitalId,wardId,.defaultClassId)
    //先判断 班次
    q:(bookItems.Count()*classItems.Count()=0) ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("没有维护班次","100021","-1")
    //快速解压。可以传入引用，按顺序"book,class,time,screening"，对应赋值
    d ##class(Nur.Tools).RapidExtract("book,class,time,screening",extras,.selectedBookId,.selectedClassId,.selectedTimeId,.selectedScreeningId)
    s bookId=$s(selectedBookId'="":selectedBookId,1:$g(defaultBookId))
    s classId=$s(selectedClassId'="":selectedClassId,1:$g(defaultClassId))
    ;;;2.初始化数据
    s defaultDate=..GetShiftDate(date,wardId,bookId,classId)
    s shiftDate=defaultDate
    ;;;3.取数据主id
    s shiftId=##class(Nur.SHIFT.Service.ShiftController).GetShiftIDByBookID(wardId,"",bookId,shiftDate,classId)
    ;;;4.取班次条目
    s shiftTimeItems=..GetTimeItems(shiftId,shiftDate,.defaultTimeId)
    s shiftTimeId=$s(selectedTimeId'="":selectedTimeId,1:$g(defaultTimeId))
    ;;;5.取统计条目和数据
    s statisticItems=..GetStatisticItems(shiftId)
    s statisticData=..GetStatisticData(shiftId,statisticItems,shiftTimeItems)
    ;;;6.取备注条目和数据
    s remarkItems=..GetRemarkItems(shiftId)
    s remarkData=..GetRemarkData(shiftId,statisticItems,shiftTimeItems)
    ;;;7.取左侧交班项目
    s shiftEventItems=..GetEventItems(shiftId)
    ;;;8.取交班明细列和数据
    s sourceItems=..GetSourceItems()
    s detailScreeningItems=..GetDetailScreeningItems(.defaultScreeningId)
    s screeningId=$s(selectedScreeningId'="":selectedScreeningId,1:$g(defaultScreeningId))
    s detailColumnItems=..GetDetailColumnItems(shiftId)
    s detailData=..GetDetailData(shiftId,detailColumnItems,sourceItems,shiftTimeId,screeningId,episodeId)
    //w ##class(Nur.JSON).Encode(detailData),!
    s patInfoList=##class(%ListOfDataTypes).%New()
    i episodeId'="" {//单患者
        s patinfo=##class(%ArrayOfDataTypes).%New()
        //基本患者信息
        s patinfo=##class(Nur.MNISV3.Service.Patient).GetBasicInfo(patinfo,episodeId)
        d patinfo.SetAt($g(detailData),"nursingBookDetailData")
        d patInfoList.Insert(patinfo)
    }else {
        //调用纯净版患者列表，
        s patListObj=##class(%ResultSet).%New("Nur.MNISV3.Service.Patient:GetPatList")
        d patListObj.Execute(wardId,userId,"Ward",locId)
        while(patListObj.Next()){
            s patinfo=##class(Nur.MNISV3.Common.Utils).ResultSet(patListObj)
            s episodeId=patListObj.GetDataByName("episodeId")
            s detailData=##class(%ListOfDataTypes).%New()
            d patinfo.SetAt($g(detailData),"nursingBookDetailData")
            //基本患者信息
            s patinfo=##class(Nur.MNISV3.Service.Patient).GetBasicInfo(patinfo,episodeId)
            d patInfoList.Insert(patinfo)
        }
    }
   
    d json.SetAt($g(Msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    d json.SetAt($g(patInfoList),"patients")
    d json.SetAt($g(bookItems),"bookItems")
    d json.SetAt($g(classItems),"classItems")  
    d json.SetAt($g(shiftTimeItems),"shiftTimeItems")  
    d json.SetAt($g(statisticItems),"statisticItems")  
    d json.SetAt($g(statisticData),"statisticData")  
    d json.SetAt($g(remarkItems),"remarkItems")  
    d json.SetAt($g(remarkData),"remarkData")   
    d json.SetAt($g(shiftEventItems),"shiftEventItems")  
    d json.SetAt($g(detailScreeningItems),"detailScreeningItems") 
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errGetNursingBookDataV4
    s Msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(Msg,"100021","-1")
}

ClassMethod GetDetailData(shiftId, detailColumnItems, sourceItems, shiftTimeId, screeningId, selectedEpisodeId) As %String
{
    n (shiftId,detailColumnItems,sourceItems,shiftTimeId,screeningId,selectedEpisodeId,%session)
    s data=##class(%ListOfDataTypes).%New()
    k admIndArr
    s rawDataStr=##class(Nur.SHIFT.Service.ShiftDetailController).GetDetailDataByField(shiftId,"","","",0,"","","","true","")
    //w rawDataStr,!
    s rawDataParent=##class(Nur.MNISV3.Common.JSON).Decode(rawDataStr)
    s rawData=rawDataParent.GetAt("data")
    s rawLen=rawData.Count()
    f rawInd=1:1:rawLen {
        s rawObj=rawData.GetAt(rawInd)
        s Content=""
        s ContentIsNotNull=0
        s detailColumnLen=detailColumnItems.Count()
        f detailColumnInd=1:1:detailColumnLen {
            s detailColumnItem=detailColumnItems.GetAt(detailColumnInd)
            s detailColumnSourceId=detailColumnItem.GetAt("ProjectTip")
            s field=detailColumnItem.GetAt("field")
            s title=detailColumnItem.GetAt("title")
            s value=rawObj.GetAt(field)
            s sourceItemLen=sourceItems.Count()
            f sourceItemInd=1:1:sourceItemLen {
                s sourceItem=sourceItems.GetAt(sourceItemInd)
                s sourceId=sourceItem.GetAt("ID")
                s sourceCode=sourceItem.GetAt("ProjectCode")
                i (sourceId=detailColumnSourceId) {
                    i (sourceCode="BedCode") s value=$replace(value,"<br>","")
                    s value=title_"："_value
                    ;连接(Concat)字符 返回Content_" "_value拼接
                    s Content=##class(Nur.Tools).Concat(Content," ",value)
                }
            }
            i (detailColumnSourceId="")&&(value'="") {
                s value=$replace(title,"列","")_"："_value
                ;连接(Concat)字符 返回Content_" "_value拼接
                s Content=##class(Nur.Tools).Concat(Content," ",value)
                s ContentIsNotNull=1
            }
        }
        continue:(screeningId=2)&&(ContentIsNotNull=0)
        s PatientID=rawObj.GetAt("PatientID")
        ;单患者- 过滤就诊号
        continue:(selectedEpisodeId'="")&&(PatientID'=selectedEpisodeId)
        s PatItemRecords=rawObj.GetAt("PatItemRecords")
        //d ..InnerInclude(PatItemRecords,"TimeID",shiftTimeId)
        s PatItemLen=PatItemRecords.Count()
        f PatItemInd=1:1:PatItemLen {
            s PatItemRecord=PatItemRecords.GetAt(PatItemInd)
            ;快速(Rapid)减轻(Lessen),只保留Set对象中的"ProjectID,ProjectName,TimeName"
            d ##class(Nur.Tools).RapidLessen("ProjectID,ProjectName,TimeName",PatItemRecord)
        }
        s PatientAllItems=rawObj.GetAt("PatientAllItems")
        s PatientAllLen=PatientAllItems.Count()
        f PatientAllInd=1:1:PatientAllLen {
            s PatientAllItem=PatientAllItems.GetAt(PatientAllInd)
            ;快速(Rapid)减轻(Lessen),只保留Set对象中的"ProjectID,ProjectName,TimeName"
            d ##class(Nur.Tools).RapidLessen("ProjectID,ProjectName,TimeName",PatientAllItem)
        }
        s obj=##class(Nur.Tools).RapidAssembly("PatItemRecords,PatientAllItems,Content,PatientID",
            PatItemRecords,PatientAllItems,Content,PatientID )
        d data.Insert(obj)
    }
    q data
}

ClassMethod GetDetailColumnItems(shiftId) As %String
{
    n (shiftId,%session)
    s items=##class(%ListOfDataTypes).%New()
    s rawItemsParent=##class(Nur.SHIFT.Service.ShiftDetailController).GetDetailGridColumns(shiftId,"")
    s rawItems=rawItemsParent.GetAt("data")
    s rawLen=rawItems.Count()
    f rawInd=1:1:rawLen {
        s rawItem=rawItems.GetAt(rawInd)
        continue:(rawItem.GetAt("editor")'=1)
        s obj=##class(Nur.Tools).RapidSubset("field,title,ProjectTip",rawItem)
        d items.Insert(obj)
    }
    q items
}

ClassMethod GetDetailScreeningItems(defaultScreeningId) As %String
{
    n (defaultScreeningId,%session)
    s keys="ScreeningName,ShiftIsDefalut,ID"
    s items=##class(Nur.Tools).AppendSet("",##class(Nur.Tools).RapidAssembly(keys,"全部患者",1,1))
    s items=##class(Nur.Tools).AppendSet(items,##class(Nur.Tools).RapidAssembly(keys,"有交班记录患者",0,2))
    s ind=1 ;..InnerLookup(items,"ShiftIsDefalut",1,-1)
    i (ind>0) {
        s defaultScreeningId=items.GetAt(ind).GetAt("ID")
    }
    q items
}

ClassMethod GetSourceItems() As %String
{
    n (%session)
    s rawItems=##class(Nur.SHIFT.Service.ShiftConfigController).GetShiftProjectList(2,"")
    //d ..InnerCollectivize(rawItems)
    s list=##class(%ListOfDataTypes).%New()
    f i=1:1:rawItems.Count(){
        s obj= ##class(Nur.Tools).RapidProp("%%OID,ProjectCode,ProjectName",rawItems.GetAt(i))
        d list.Insert(obj)
    }
    q list
    //q ..InnerSubset(rawItems,"ID,ProjectCode,ProjectName")
}

ClassMethod GetEventItems(shiftId) As %String
{
    n (shiftId,%session)
    s rawDataParent=##class(Nur.SHIFT.Service.ShiftDetailController).GetDetailLeftProjectData(shiftId)
    s rawItems=rawDataParent.GetAt("data")
    /// 内部抽取子集：只获取keys定义的数据
    q ##class(Nur.Tools).InnerSubset(rawItems,"ProjectID,ProjectName,ProjectValue,field")
}

ClassMethod GetRemarkData(shiftId, remarkItems, timeItems) As %String
{
    n (shiftId,remarkItems,timeItems,%session)
    s data=##class(%ListOfDataTypes).%New()
    s rawData=##class(Nur.SHIFT.Service.ShiftCustomController).GetCustomData(shiftId,"")
    s rawLen=rawData.Count()
    s timeItemLen=timeItems.Count()
    f timeItemInd=1:1:timeItemLen {
        s timeItem=timeItems.GetAt(timeItemInd)
        s timeItemId=timeItem.GetAt("ID")
        s timeName=timeItem.GetAt("ShiftName")
        s timeColor=timeItem.GetAt("ShiftColor")
        s subdata=##class(%ListOfDataTypes).%New()
        f rawInd=1:1:rawLen {
            s rawObj=rawData.GetAt(rawInd)
            s remarkId=rawObj.GetAt("LogCustomID")
            s remarkName=rawObj.GetAt("CustomName")
            
            s remarkData=rawObj.GetAt("CustomList")
            s remarkLen=remarkData.Count()
            f remarkInd=1:1:remarkLen {
                s remarkObj=remarkData.GetAt(remarkInd)
                s timeId=remarkObj.GetAt("TimeID")
                i (timeId=timeItemId) {
                    s value=remarkObj.GetAt("ShiftContent")
                    i (value'="") {
                        ;快速组装一个{}
                        s subobj=##class(Nur.Tools).RapidAssembly("RemarkName,LogCustomID,ShiftContent",remarkName,remarkId,value)
                        d subdata.Insert(subobj)
                    }
                    q
                }
            }
        }
        continue:(subdata.Count()=0)
        s obj=##class(Nur.Tools).RapidAssembly("TimeName,TimeColor,TimeID,TimeData",timeName,timeColor,timeId,subdata)
        d data.Insert(obj)
    }
    q data
}

ClassMethod GetRemarkItems(shiftId) As %String
{
    n (shiftId,%session)
    s items=##class(%ListOfDataTypes).%New()
    s rawItemsParent=##class(Nur.SHIFT.Service.ShiftCustomController).GetCustom(shiftId,"")
    s rawItems=rawItemsParent.GetAt("data")
    k sortArr
    s rawLen=rawItems.Count()
    f rawInd=1:1:rawLen {
        s rawItem=rawItems.GetAt(rawInd)
        ; 快速解压。可以传入引用，并对引用赋值
        d ##class(Nur.Tools).RapidExtract("AreaRow,AreaCol",rawItem,.row,.col)
        ;快速(Rapid)抽取子集(Subset)
        s obj=##class(Nur.Tools).RapidSubset("AreaCode,AreaName,LogCustomID",rawItem)
        s sortArr(+row,+col,rawInd)=obj
    }
    s row="" f  s row=$o(sortArr(row)) q:(row="")  d
    .s col="" f  s col=$o(sortArr(row,col)) q:(col="")  d
    ..s rawInd="" f  s rawInd=$o(sortArr(row,col,rawInd)) q:(rawInd="")  d
    ...s obj=$g(sortArr(row,col,rawInd))
    ...d items.Insert(obj)
    q items
}

ClassMethod GetStatisticData(shiftId, statisticItems, timeItems) As %String
{
    n (shiftId,statisticItems,timeItems,%session)
    s data=##class(%ListOfDataTypes).%New()
    s rawDataParent=##class(Nur.SHIFT.Service.ShiftProjectController).GetAreaGridData(shiftId)
    s rawObjLast=##class(Nur.SHIFT.Service.ShiftProjectController).GetLastAreaGridData(shiftId)
    s rawData=rawDataParent.GetAt("data")
    s rawLen=rawData.Count()
    s timeItemLen=timeItems.Count()
    f timeItemInd=1:1:timeItemLen {
        s timeItem=timeItems.GetAt(timeItemInd)
        s timeItemId=timeItem.GetAt("ID")
        s timeName=timeItem.GetAt("ShiftName")
        s subdata=##class(%ListOfDataTypes).%New()
        ;;;原数据就是按timeItems排列的，这里只是象征性匹配一下
        f rawInd=1:1:rawLen {
            s rawObj=rawData.GetAt(rawInd)
            i (rawInd=1) d ##class(Nur.Tools).MergeSet(rawObj,rawObjLast) ;合并两个对象
            s timeId=rawObj.GetAt("TimeID")
            i (timeId=timeItemId) {
                s statisticItemLen=statisticItems.Count()
                f statisticItemInd=1:1:statisticItemLen {
                    s statisticItem=statisticItems.GetAt(statisticItemInd)
                    s field=statisticItem.GetAt("field")
                    s title=statisticItem.GetAt("title")
                    s value=rawObj.GetAt(field)
                    s subobj=##class(Nur.Tools).RapidAssembly("title,field,value",title,field,value) ;快速组装一个{}
                    d subdata.Insert(subobj)
                }
                q
            }
        }
        continue:(subdata.Count()=0)
        ;快速组装一个{}
        s obj=##class(Nur.Tools).RapidAssembly("TimeName,TimeID,TimeData",timeName,timeId,subdata)
        d data.Insert(obj)
    }
    q data
}

ClassMethod GetStatisticItems(shiftId) As %String
{
    n (shiftId,%session)
    s rawDataParent=##class(Nur.SHIFT.Service.ShiftProjectController).GetAreaGridColumns(shiftId,1)
    s rawItems=rawDataParent.GetAt("data")
    /// 内部抽取子集：只获取keys定义的数据
    q ##class(Nur.Tools).InnerSubset(rawItems,"field,title,ProjectID")
}

ClassMethod GetTimeItems(shiftId, shiftDate, ByRef defaultTimeId) As %String
{
    n (shiftId,shiftDate,defaultTimeId,%session)
    s rawDataParent=##class(Nur.SHIFT.Service.ShiftController).GetAreaShiftTime(shiftId)
    s rawItems=rawDataParent.GetAt("data")
    s defaultTimeId=rawDataParent.GetAt("thisTimeID")
    //d ..InnerCollectivize(rawItems)
    i (defaultTimeId="") {
        s len=##class(Nur.Tools).Size(rawItems)
        i (len>0) {
            s dateVal=##class(Nur.Tools).Date(shiftDate)
            i (dateVal<+$h) s ind=len
            e  s ind=1
            s defaultTimeId=rawItems.GetAt(ind).%Id()
        }
    }
    s list=##class(%ListOfDataTypes).%New()
    f i=1:1:rawItems.Count(){
        s obj= ##class(Nur.Tools).RapidProp("%%OID,ShiftName,ShiftColor,ShiftProjectIDSy",rawItems.GetAt(i))
        d list.Insert(obj)
    }
     q list
    /// 内部抽取子集：只获取keys定义的数据
    //q ##class(Nur.Tools).InnerSubset(rawItems,"ShiftName,ShiftColor,ShiftProjectIDSy,ID")
}

ClassMethod GetShiftDate(date, wardId, bookId, classId) As %String
{
    n (date,wardId,bookId,classId,%session)
    s rawDataParent=##class(Nur.SHIFT.Service.ShiftController).GetShiftDate(wardId,"",bookId,date,classId)
    q ##class(Nur.Tools).GetAt(rawDataParent,"data")
}

/// w ##class(Nur.MNISV3.Service.Shift).GetBookItems(2,3)
ClassMethod GetBookItems(hospId, wardId, ByRef defaultBookId) As %String
{
    n (hospId,wardId,defaultBookId,%session)
    s items=##class(Nur.SHIFT.Service.ShiftConfigController).GetShiftBookByWardList(hospId,wardId)
    s list=##class(%ListOfDataTypes).%New()
    i (items.Count()>0) {
        //d ..InnerAssign(items,"ShiftIsDefalut",0)
        //d ..Cascading(items,".GetAt(1).SetAt(1,""ShiftIsDefalut"")")
        s defaultBookId=items.GetAt(1).%Id()
        f i=1:1:items.Count(){
            s obj= ##class(Nur.Tools).RapidProp("%%OID,ShiftBookName,ShiftBookType",items.GetAt(i))
            d list.Insert(obj)
        }
    }
    q list
}

ClassMethod GetClassItems(hospId, wardId, ByRef defaultClassId) As %String
{
    n (hospId,wardId,defaultClassId,%session)
    s hospItems=##class(Nur.SHIFT.Service.ShiftConfigController).GetShiftClassList(hospId,"","")
    s wardItems=##class(Nur.SHIFT.Service.ShiftConfigController).GetShiftClassList(hospId,wardId,"")
    //d ..InnerAssign(hospItems,"isWard",0)
    //d ..InnerAssign(wardItems,"isWard",1)
    s items=##class(Nur.Tools).MergeSet(hospItems,wardItems)
    s list=##class(%ListOfDataTypes).%New()
    s ind=0 ;..InnerLookup(items,"ShiftIsDefalut",1,-1)
    //d ..InnerAssign(items,"ShiftIsDefalut",0)
    s:(ind=0)&&(items.Count()>0) ind=1
    i (ind>0) {
        //d ..Cascading(items,".GetAt(ind).SetAt(1,""ShiftIsDefalut"")")
        s defaultClassId=items.GetAt(ind).%Id()
    }
    f i=1:1:items.Count(){
        s obj= ##class(Nur.Tools).RapidProp("%%OID,ShiftClassName,ShiftClassType,ShiftIsDefalut",items.GetAt(i))
        d list.Insert(obj)
    }
    q list
}

}
